#ifndef LOGIC_CONTROLLER_H
#define LOGIC_CONTROLLER_H

// provided interface
#include "logicctl.h"
// required interface
#include "globaldata.h"
// basic component model
#include <bcm/bcm.h>

namespace cnc {

	namespace plc {

		class LogicController : public bcm::Identifier<bcm::Component>,
			public bcm::Identifier<LogicControl>
		{
				cnc::base::GlobalData *gd_;
			protected:
				LogicController() : bcm::Identifier<bcm::Component>("lc", "Logic Controller"),
					bcm::Identifier<LogicControl>("logicctl", "Logic Control"),
				gd_(0) {}
			protected: // bcm::Component
				virtual bcm::IdentifierData *getProvidedInterfaceTable()
				{
					static bcm::IdentifierData idData[] = {
						{ "logicctl", "Logic Control" },
						{ 0, 0 }
					};
					return idData;
				}
				virtual bcm::IdentifierData *getRequiredInterfaceTable()
				{
					static bcm::IdentifierData idData[] = {
						{ "gdata", "Global Data"},
						{ 0, 0 }
					};
					return idData;
				}
				virtual bcm::Interface *getInterface(const char *id)
				{
					if (std::string("logicctl") == id)
						return static_cast<LogicControl *>(this);
					return 0;
				}
				virtual bool setInterface(Interface *iface)
				{
					if (iface == 0)
						return false;
					if (std::string("gdata") == iface->getID()) {
						cnc::base::GlobalData *gd = dynamic_cast<cnc::base::GlobalData *>(iface);
						if (gd == 0)
							return false;
						gd_ = gd;
					} else
						return false;
					return true;
				}
			public:
				static LogicController &instance()
				{
					static LogicController object;
					return object;
				}
		};

	} // namespace plc

} // namespace cnc
#endif
